from sqlalchemy import Column, Integer, String, Date, DateTime, Numeric
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship

from app.core.database import Base


class Stock(Base):
    """股票基础信息模型"""
    __tablename__ = "stocks"
    
    id = Column(Integer, primary_key=True, index=True)
    code = Column(String(20), unique=True, index=True, nullable=False)  # 股票代码
    name = Column(String(100), nullable=False)  # 股票名称
    market = Column(String(10), nullable=False)  # 市场 SH/SZ/BJ
    industry = Column(String(50))  # 所属行业
    sector = Column(String(50))  # 所属板块
    listing_date = Column(Date)  # 上市日期
    total_shares = Column(Numeric(20, 2))  # 总股本
    float_shares = Column(Numeric(20, 2))  # 流通股本
    market_cap = Column(Numeric(20, 2))  # 总市值
    pe_ratio = Column(Numeric(10, 2))  # 市盈率
    pb_ratio = Column(Numeric(10, 2))  # 市净率
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
    
    # 关系
    watchlists = relationship("UserWatchlist", back_populates="stock")
    
    def __repr__(self):
        return f"<Stock(id={self.id}, code='{self.code}', name='{self.name}', market='{self.market}')>"